package com.haidechizi.i18n.code.generator.common.propconvert.generator;


import com.haidechizi.i18n.code.generator.common.message.utils.FileUtils;
import com.haidechizi.i18n.code.generator.common.message.utils.RegexUtils;
import com.haidechizi.i18n.code.generator.common.propconvert.param.PropConvertConfig;

import java.io.File;
import java.io.FileInputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * @author wangdejun
 * @create 2020/10/17 9:50
 */
public class JavaPropertiesGenerator extends BasePropGenerator {

    /**
     * 生成国际化资源文件
     * 乱序，不采用此方式
     *
     * @param propConvertConfig 参数
     * @param languageMap       对应语言的资源内容
     * @param messageMap        原始文件的资源内容
     */
    /*@Override
    protected void generatorFile(PropConvertConfig propConvertConfig, Map<String,
            Map<String, String>> languageMap, Map<String, String> messageMap) {
        for (Map.Entry<String, Map<String, String>> entry : languageMap.entrySet()) {
            String filePath = propConvertConfig.getFilePath().replace(propConvertConfig.getCurrentLanguage(), entry.getKey());
            StringBuilder sb = new StringBuilder();
            for (Map.Entry<String, String> messageEntry : entry.getValue().entrySet()) {
                sb.append(messageEntry.getKey()).append("=").append(messageEntry.getValue()).append("\n");
            }
            FileUtils.createFile(sb.toString(), filePath, ".new");
        }

    }*/

    /**
     * 解析静态资源文件，提取国际化数据
     *
     * @param propConvertConfig 参数
     * @return
     */
    @Override
    protected Map<String, String> generatorMessage(PropConvertConfig propConvertConfig) {
        Map<String, String> messageMap = new HashMap<>();
        File file = new File(propConvertConfig.getFilePath());
        Properties properties = new Properties();
        try (FileInputStream fis = new FileInputStream(file)) {
            properties.load(fis);
        } catch (Exception e) {
            throw new RuntimeException("解析Properties文件出现异常");
        }
        for (Map.Entry<Object, Object> entry : properties.entrySet()) {
            messageMap.put(String.valueOf(entry.getKey()), String.valueOf(entry.getValue()));
        }
        return messageMap;
    }

    @Override
    public String type() {
        return "JavaProperties";
    }

    /**
     * 判断是否支持当前类型文件的操作
     *
     * @param suffix
     * @return
     */
    @Override
    public boolean support(String suffix) {
        return "properties".equals(suffix);
    }

    @Override
    protected String generatorReplaceRegex(String regex, Map.Entry<String, String> messageEntry) {
        return RegexUtils.parseRegex(messageEntry.getKey()) + "(\\s)*:(\\s)*" + regex;
    }

    @Override
    protected String generatorReplaceData(Map.Entry<String, String> messageEntry) {
        return messageEntry.getKey() + "=" + messageEntry.getValue();
    }

    @Override
    protected String beforeGenerator(PropConvertConfig propConvertConfig, String resourcesContent) {
        return FileUtils.unicodeToChina(resourcesContent);
    }

    @Override
    protected String afterGenerator(PropConvertConfig propConvertConfig, String content,String language) {
        return FileUtils.chinaToUnicode(content);
    }
}
